<?php
/**
 * Author:xsc
 * Time:2023/9/4 16:37
 */

namespace App\Repository;

use App\Models\SpecialPrivilege;

class SpecialPrivilegeRepo extends BaseRepo
{

    /**
     * 新增/更新赋能
     * @param array $data
     * @param int $id
     * @return int
     * Author:xsc
     * Time:2023/9/6 15:56
     */
    public function save(array $data, int $id = 0)
    {
        if ($id) {
            return SpecialPrivilege::query()->where('id', $id)->update($data);
        } else {
            return SpecialPrivilege::query()->insertGetId($data);
        }
    }

    /**
     * 赋能列表
     * @param int $special_id
     * @return array
     * Author:xsc
     * Time:2023/10/7 16:44
     */
    public function list(int $special_id)
    {
        $where = [
            ['special_id', '=', $special_id],
        ];
        $res = SpecialPrivilege::query()->where($where)->get()->toArray();
        return $res;
    }

    public function specialIsExistPrivilege($special_id): int
    {
        return SpecialPrivilege::query()->where("special_id",$special_id)
            ->where("status",1)->exists() ? 1 : 0;
    }

    /**
     * 获取赋能列表
     * @param array $params
     * @return array
     * Author:xsc
     * Time:2023/10/25 11:30
     */
    public function getList(array $params)
    {
        $where = [];
        if (isset($params['special_id'])) {
            $where[] = ['special_id', '=', $params['special_id']];
        }
        if (isset($params['status'])) {
            $where[] = ['status', '=', $params['status']];
        }
        $res = SpecialPrivilege::query()->where($where)->get()->toArray();
        return $res;
    }

    /**
     * 获取赋能信息
     * @param int $id
     * @return array
     * Author:xsc
     * Time:2023/10/25 13:30
     */
    public function getInfo(int $id)
    {
        $res = SpecialPrivilege::query()->where('id', $id)->first();
        return $res ? $res->toArray() : [];
    }

    /**
     * 更新已领取数量
     * @param int $id
     * @param int $num
     * @return int
     * Author:xsc
     * Time:2023/11/9 9:40
     */
    public function updateUseNum(int $id, int $num = 1)
    {
        return SpecialPrivilege::query()->where('id', $id)->increment('use_num', $num);
    }

}
